package com.shortUrl.starter.util;

import io.vertx.core.Vertx;
import io.vertx.core.json.JsonObject;
import io.vertx.redis.client.RedisAPI;
import io.vertx.redis.client.RedisOptions;
import io.vertx.redis.client.impl.RedisClient;

public class RedisUtils {
    private static RedisClient redisClient;
    private static final RedisOptions options = new RedisOptions();
    private static byte[] lock = {};
    static {

        try {
            JsonObject jsonObject =  LoadConfigUtils.getConfig();
            String url = jsonObject.getJsonObject("redis").getString("redisUrl");
            options.setConnectionString(url);
            System.out.println(url);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static RedisClient getRedisClient(Vertx vertx) {
        if (redisClient == null) {
            synchronized (lock) {
                if (redisClient == null) {
                    redisClient = new RedisClient(vertx, options);
                }
            }
        }
        return redisClient;
    }
    public static RedisAPI getRedisAPI(Vertx vertx){
        return RedisAPI.api(getRedisClient(vertx));
    }
}